//list页面控制器
angular.module('listModule.controller',['listModule.service'])
    .controller('listController',['$scope','$stateParams','listFactory',function($scope,$stateParams,listFactory){
        $scope.type=$stateParams.type;
        $scope.activeType=function(){
            $scope.$emit('active-Type',$scope.type)
        }

        $scope.moviesList={}
        $scope.loading=true
        //根据分页需求抽象分页模型
        $scope.page={
            count:5,//每页显示的条数
            start:0,//从第几条开始请求
            current:1,//当前第几页
            totalCount:0,//一共总条数
            totalPage:0//总页数
        }

        //上一页
        $scope.goBack = function(){
            if($scope.page.current <= 1){
                return false;
            }
            $scope.page.current--
            $scope.getData()
        }

        //下一页
        $scope.goForward = function(){
            if($scope.page.current >= $scope.page.totalPage){
                return false;
            }
            $scope.page.current++
            $scope.getData()
        }

        $scope.getData=function(){
            $scope.activeType();
            $scope.loading=true
            listFactory.getData({count:$scope.page.count,start:$scope.page.count*($scope.page.current-1),city:'上海'},function(data){
                $scope.moviesList = data;
                $scope.loading=false;

                $scope.page.totalCount=data.total;
                $scope.page.totalPage=Math.ceil(data.total/$scope.page.count)

                $scope.$apply();
            })
        }

        $scope.getData();

        //搜索功能
        $scope.$on('search-btn',function(event,data){
            $scope.seachData=data
            $scope.loading=true
            listFactory.getSearch({q:$scope.seachData},function(data){
                $scope.moviesList = data;
                $scope.loading=false;

                $scope.page.totalCount=data.total;
                $scope.page.totalPage=Math.ceil(data.total/$scope.page.count)

                $scope.$apply();
            })
        })

    }])
